<template>
	<div class="app-container">
		<!-- 搜索 -->
		<div class="filter-container tw-filter">
			<el-button class="filter-item tw-filter-button" size="medium" icon="el-icon-edit" v-has="$store.state.user.btn_common+'add'" @click="handleAdd()">添加</el-button>
			<el-button class="filter-item tw-filter-button" size="medium" icon="el-icon-delete" v-has="$store.state.user.btn_common+'del'" @click="handleDelete()">批量删除</el-button>

			<el-select v-model="queryList.type" class="filter-item tw-filter-input" placeholder="请选择模板类型">
				<el-option v-for="item in templateListAll" :key="item.key" filter="false" :label="item.value" :value="item.key"></el-option>
			</el-select>
			<!-- <el-select v-model="queryList.is_inside" class="filter-item tw-filter-input" placeholder="请选择是否站内消息">
				<el-option key="" label="全部" value="" />
				<el-option key="1" label="是" value="1" />
				<el-option key="0" label="否" value="0" />
			</el-select> -->
			<el-select v-model="queryList.is_sms" class="filter-item tw-filter-input" placeholder="请选择是否发送短信">
				<el-option key="" label="全部" value="" />
				<el-option key="1" label="是" value="1" />
				<el-option key="0" label="否" value="0" />
			</el-select>
			<el-input v-model="queryList.sms_signature" placeholder="短信签名" class="filter-item tw-filter-input" />
			<el-input v-model="queryList.sms_code" placeholder="短信模板" class="filter-item tw-filter-input" />
			<!-- <el-select v-model="queryList.is_mail" class="filter-item tw-filter-input" placeholder="请选择是否发送邮件">
				<el-option key="" label="全部" value="" />
				<el-option key="1" label="是" value="1" />
				<el-option key="0" label="否" value="0" />
			</el-select> -->
     <!-- <el-select v-model="queryList.is_wechat" class="filter-item tw-filter-input" placeholder="请选择是否发送公众号消息">
      	<el-option key="" label="全部" value="" />
      	<el-option key="1" label="是" value="1" />
      	<el-option key="0" label="否" value="0" />
      </el-select>
      <el-select v-model="queryList.is_wechat_small" class="filter-item tw-filter-input" placeholder="请选择是否发送小程序消息">
      	<el-option key="" label="全部" value="" />
      	<el-option key="1" label="是" value="1" />
      	<el-option key="0" label="否" value="0" />
      </el-select> -->
			<el-select v-model="queryList.state" class="filter-item tw-filter-input" placeholder="请选择状态">
				<el-option key="" label="全部状态" value="" />
				<el-option key="1" label="启用" value="1" />
				<el-option key="0" label="禁用" value="0" />
			</el-select>

			<el-button v-waves class="filter-item tw-filter-button" size="medium" icon="el-icon-search" @click="handleFilter">搜索</el-button>
		</div>

		<!-- 表格内容 -->
    <el-table
      v-loading="listLoading"
      :data="tableList"
      @selection-change="handleSelectionChange"
      border fit highlight-current-row style="width: 100%;"
    >
			<el-table-column type="selection" width="50" />
			<el-table-column label="排序" prop="sort_id" align="center" width="80" v-has="$store.state.user.btn_common+'edit'">
				<template slot-scope="{row}">
					<el-input v-model="row.sort_id" class="edit-input" size="small" @change="updateSort(row.id, row.sort_id)" />
				</template>
			</el-table-column>
      <el-table-column label="模板类型" align="left" width="150">
        <template slot-scope="{row}">
          <span>{{ row.type_name}}【{{ row.type}}】</span>
        </template>
      </el-table-column>
			<!-- <el-table-column label="发送站内消息" align="center" width="120">
				<template slot-scope="{row}">
					<span v-if="row.is_inside == 1" style="color:#090">是</span>
					<span v-else style="color:#ff0000">否</span>
				</template>
			</el-table-column>
			<el-table-column label="消息标题" prop="inside_title" align="left" width="120" />
			<el-table-column label="消息内容" prop="inside_details" align="left" width="180" />
			<el-table-column label="站内类型" prop="inside_type_name" align="center" width="120" /> -->
			<el-table-column label="发送短信" align="center" width="120">
				<template slot-scope="{row}">
					<span v-if="row.is_sms == 1" style="color:#090">是</span>
					<span v-else style="color:#ff0000">否</span>
				</template>
			</el-table-column>
			<el-table-column label="短信签名" prop="sms_signature" width="120" align="left" />
			<el-table-column label="短信模板" prop="sms_code" width="120" align="left" />
			<el-table-column label="短信内容" prop="sms_details" align="left" width="180" />
			<!-- <el-table-column label="发送邮件" align="center" width="120">
				<template slot-scope="{row}">
					<span v-if="row.is_mail == 1" style="color:#090">是</span>
					<span v-else style="color:#ff0000">否</span>
				</template>
			</el-table-column>
			<el-table-column label="邮件标题" prop="mail_title" align="left" width="120" />
			<el-table-column label="发送公众号模板消息" width="120" align="center">
				<template slot-scope="{row}">
					<span v-if="row.is_wechat == 1" style="color:#090">是</span>
					<span v-else style="color:#ff0000">否</span>
				</template>
			</el-table-column>
			<el-table-column label="模板ID" prop="wechat_template" align="left" width="120" />
			<el-table-column label="跳转url" prop="wechat_url" width="120" align="left" />
			<el-table-column label="发送小程序模板消息" align="center" width="120">
				<template slot-scope="{row}">
					<span v-if="row.is_wechat_small == 1" style="color:#090">是</span>
					<span v-else style="color:#ff0000">否</span>
				</template>
			</el-table-column>
			<el-table-column label="模板ID" prop="wechat_small_template" width="120" align="left" />
			<el-table-column label="跳转url" prop="wechat_small_url" width="120" align="left" /> -->
			<el-table-column label="状态" align="center">
				<template slot-scope="{row}">
					<span v-if="row.state == 1" style="color:#090">启用</span>
					<span v-else style="color:#ff0000">禁用</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="220" align="center" fixed="right" class-name="small-padding fixed-width">
				<template slot-scope="{row}">
					<el-button size="mini" v-has="$store.state.user.btn_common+'edit'" @click="handleEdit(row.id)">编辑</el-button>
					<el-button size="mini" v-has="$store.state.user.btn_common+'upshow'" v-if="row.state" @click="handleUpshow(row.id)">禁用</el-button>
					<el-button size="mini" v-has="$store.state.user.btn_common+'show'" v-else @click="handleShow(row.id)">启用</el-button>
					<el-button size="mini" v-has="$store.state.user.btn_common+'del'" @click="handleDelete(row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<pagination v-show="isPage && listTotal > 0" :total="listTotal" :page.sync="queryList.curpage" :limit.sync="queryList.page" @pagination="getList" />

		<el-dialog width="45%" @close="resetForm()" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
		  <el-form label-position="right" label-width="180px" ref="dataForm" :rules="rules" :model="form">
				<el-form-item label="模板类型" prop="type">
					<el-select v-model="form.type" class="filter-item" placeholder="请选择模板类型" style="display:block;">
						<el-option v-for="item in templateList" :key="item.key" filter="false" :label="item.value" :value="item.key"></el-option>
					</el-select>
				</el-form-item>

				<!-- <el-form-item label="是否发送站内消息" prop="is_inside">
					<el-radio-group v-model="form.is_inside">
						<el-radio :label="1">是</el-radio>
						<el-radio :label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="站内消息标题" prop="inside_title" v-if="form.is_inside">
					<el-input v-model="form.inside_title" placeholder="请输入站内消息标题" />
				</el-form-item>
				<el-form-item label="站内消息内容" prop="inside_details" v-if="form.is_inside">
					<el-input v-model="form.inside_details" type="textarea" :rows="5" maxlength="300" placeholder="请输入站内消息内容" />
				</el-form-item>
				<el-form-item label="站内消息类型" prop="inside_type" v-if="form.is_inside">
					<el-select v-model="form.inside_type" class="filter-item" placeholder="请选择消息类型" style="display:block;">
						<el-option v-for="item in insideTypeList" :key="item.key" filter="false" :label="item.value" :value="item.key"></el-option>
					</el-select>
				</el-form-item> -->

				<el-form-item label="是否发送短信" prop="is_sms">
					<el-radio-group v-model="form.is_sms">
						<el-radio :label="1">是</el-radio>
						<el-radio :label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="短信签名" prop="sms_signature" v-if="form.is_sms">
					<el-input v-model="form.sms_signature" placeholder="请输入短信标题" />
					<span style="color: grey;">与阿里短信内短信签名保持一致</span>
				</el-form-item>
				<el-form-item label="短信模板" prop="sms_code" v-if="form.is_sms">
					<el-input v-model="form.sms_code" placeholder="请输入短信标题" />
					<span style="color: grey;">与阿里短信内短信模板code保持一致</span>
				</el-form-item>
				<el-form-item label="短信内容" prop="sms_details" v-if="form.is_sms">
					<el-input v-model="form.sms_details" type="textarea" :rows="5" maxlength="200" placeholder="请输入短信内容" />
					<span style="color: grey;">与阿里短信内短信内容保持一致</span>
				</el-form-item>

				<!-- <el-form-item label="是否发送邮件" prop="is_mail">
					<el-radio-group v-model="form.is_mail">
						<el-radio :label="1">是</el-radio>
						<el-radio :label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="邮件标题" prop="mail_title" v-if="form.is_mail">
					<el-input v-model="form.mail_title" placeholder="请输入邮件标题" />
				</el-form-item>
				<el-form-item label="邮件消息内容" prop="mail_details" v-if="form.is_mail">
					<el-input v-model="form.mail_details" type="textarea" :rows="5" maxlength="500" placeholder="请输入邮件消息内容" />
				</el-form-item> -->

				<!-- <el-form-item label="是否发送公众号模板消息" prop="is_wechat">
					<el-radio-group v-model="form.is_wechat">
						<el-radio :label="1">是</el-radio>
						<el-radio :label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="模板ID" prop="wechat_template" v-if="form.is_wechat">
					<el-input v-model="form.wechat_template" placeholder="请输入模板ID" />
				</el-form-item>
				<el-form-item label="跳转url" prop="wechat_url" v-if="form.is_wechat">
					<el-input v-model="form.wechat_url" placeholder="请输入跳转url" />
					<span style="color: grey;">系统内页面可以不加域名“以/开始”</span>
				</el-form-item> -->

				<!-- <el-form-item label="是否发送小程序模板消息" prop="is_wechat_small">
					<el-radio-group v-model="form.is_wechat_small">
						<el-radio :label="1">是</el-radio>
						<el-radio :label="0">否</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="模板ID" prop="wechat_small_template" v-if="form.is_wechat_small">
					<el-input v-model="form.wechat_small_template" placeholder="请输入模板ID" />
				</el-form-item>
				<el-form-item label="跳转url" prop="wechat_small_url" v-if="form.is_wechat_small">
					<el-input v-model="form.wechat_small_url" placeholder="请输入跳转url" />
					<span style="color: grey;">以/开始</span>
				</el-form-item> -->

				<el-form-item label="状态" prop="state">
					<el-radio-group v-model="form.state">
						<el-radio :label="1">启用</el-radio>
						<el-radio :label="0">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="排序" prop="sort_id">
					<el-input v-model="form.sort_id" />
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogStatus==='create'?createData():updateData()">保存</el-button>
				<el-button @click="dialogFormVisible = false">取消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import Pagination from '@/components/Pagination' // 基于el分页的二次封装
	import waves from '@/directive/waves' // waves directive

	export default {
		components: { Pagination },
		directives: { waves },
		data() {
			return {
				// 数据列表
				listLoading: true,
				listTotal: 0,
				isPage: false,
				tableList: [],
				multipleSelection: [],//多选数据
				// 添加/编辑弹窗
				dialogFormVisible: false,
				dialogStatus: '',
				textMap: {
					update: '编辑',
					create: '添加'
				},
        //键字段
        keyName: 'id',
        apiPath: 'project.noticetemplate',

				// 搜索数据
				queryList: {
					page: this.$store.state.settings.page, // 每页显示数量
					curpage: 1, // 当前页码
					type: '', //模板类型
					is_inside: null, //是否发送站内消息 0-否 1-是
					is_sms: null, //是否发送短信 0-否 1-是
					sms_signature: '', //短信签名
					sms_code: '', //短信模板
					is_mail: null, //是否发送邮件 0-否 1-是
					is_wechat: null, //是否发送公众号模板消息 0-否 1-是
					is_wechat_small: null, //是否发送小程序模板消息 0-否 1-是
					state: null, //状态，0禁用，1启用
				},

				// 表单数据
				form: {
          id: '',
					type: '', //模板类型
					is_inside: 0, //是否发送站内消息 0-否 1-是
					inside_title: '', //站内消息标题
					inside_details: '', //站内消息内容
					inside_type: '', //站内消息类型
					is_sms: 0, //是否发送短信 0-否 1-是
					sms_signature: '', //短信签名
					sms_code: '', //短信模板
					sms_details: '', //短信内容
					is_mail: 0, //是否发送邮件 0-否 1-是
					mail_title: '', //邮件标题
					mail_details: '', //邮件消息内容
					is_wechat: 0, //是否发送公众号模板消息 0-否 1-是
					wechat_template: '', //公众号模板ID
					wechat_url: '', //跳转url
					is_wechat_small: 0, //是否发送小程序模板消息 0-否 1-是
					wechat_small_template: '', //小程序模板ID
					wechat_small_url: '', //跳转url（以/开始）
					state: 1, //状态，0禁用，1启用
					sort_id: 255 //排序
				},
				// 重置数据
				formReset: {
          id: '',
          type: '', //模板类型
          is_inside: 0, //是否发送站内消息 0-否 1-是
          inside_title: '', //站内消息标题
          inside_details: '', //站内消息内容
          inside_type: '', //站内消息类型
          is_sms: 0, //是否发送短信 0-否 1-是
          sms_signature: '', //短信签名
          sms_code: '', //短信模板
          sms_details: '', //短信内容
          is_mail: 0, //是否发送邮件 0-否 1-是
          mail_title: '', //邮件标题
          mail_details: '', //邮件消息内容
          is_wechat: 0, //是否发送公众号模板消息 0-否 1-是
          wechat_template: '', //公众号模板ID
          wechat_url: '', //跳转url
          is_wechat_small: 0, //是否发送小程序模板消息 0-否 1-是
          wechat_small_template: '', //小程序模板ID
          wechat_small_url: '', //跳转url（以/开始）
          state: 1, //状态，0禁用，1启用
          sort_id: 255 //排序
				},
				// 验证
				rules: {
					type: [{ required: true, message: '请选择模板类型！', trigger: 'blur' }],
				},

				// 消息模板-类型-列表
				templateListAll: [],
				templateList: [],

				// 站内消息-类型-列表
				insideTypeListAll: [],
				insideTypeList: [],
			}
		},
		created() {
			//获取数据列表
			this.getList();
			// 获取消息模板-类型-列表
			this.getTemplate()
			// 获取站内消息-类型-列表
			this.getInside()
		},
		methods: {
      // 消息模板-类型-列表
      getTemplate() {
      	this.$api.fetchListDictionary({ident: 'notice_template_type'}).then(response => {
      		this.templateList = JSON.parse(JSON.stringify(response.list));
      		// 数据处理
      		response.list.unshift({
      			key: '0',
      			value: '全部模板类型'
      		})
      		this.templateListAll = response.list
      	})
      },
      // 站内消息-类型-列表
      getInside() {
      	this.$api.fetchListDictionary({ident: 'notice_inside_type'}).then(response => {
      		this.insideTypeList = JSON.parse(JSON.stringify(response.list));
      		// 数据处理
      		response.list.unshift({
      			key: '0',
      			value: '全部站内消息类型'
      		})
      		this.insideTypeListAll = response.list
      	})
      },
/*******************************公共方法******************************/
      // 搜索
      handleFilter() {
      	this.queryList.curpage = 1
      	this.tableList = []
        this.listTotal = 0
      	this.getList()
      },
      // 列表选择
      handleSelectionChange(val) {
      	this.multipleSelection = val
      },
      // 重置form
      resetForm() {
      	this.form = JSON.parse(JSON.stringify(this.formReset))
      },
			// 获取数据列表
			getList() {
				this.listLoading = true
				this.$api.fetchList(this.queryList, this.apiPath).then(response => {
					this.tableList = response.list
					this.listTotal = response.list_count
					this.isPage = response.is_page
          this.listLoading = false
				})
			},
			// 添加
			handleAdd() {
			  this.resetForm()
			  this.dialogStatus = 'create'
			  this.dialogFormVisible = true
			  this.$nextTick(() => {
			    this.$refs['dataForm'].clearValidate()
			  })
			},
			// 编辑
			handleEdit(id = 0) {
			  this.resetForm()
			  // 获取详情
			  this.$api.getDetail({ id: id }, this.apiPath).then(response => {
          //数据处理
          for(let key in this.form){
            if(response.detail.hasOwnProperty(key)){
              this.form[key] = response.detail[key]
            }
          }
			  })
			  this.dialogStatus = 'update'
			  this.dialogFormVisible = true
			  this.$nextTick(() => {
			    this.$refs['dataForm'].clearValidate();
			  })
			},
      // 添加-数据提交
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.add(this.form, this.apiPath).then(() => {
              // 重新获取列表
              this.queryList.curpage = 1
              this.tableList = []
              this.getList()

              this.dialogFormVisible = false
              this.$message({
                message: '添加成功',
                type: 'success'
              })
            })
          }
        })
      },
      // 编辑-数据提交
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$api.edit(this.form, this.apiPath).then(response => {
              // 重新获取列表
              this.getList()

              this.dialogFormVisible = false
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
            })
          }
        })
      },
      // 删除
      handleDelete(id = 0) {
        let current = this
        let id_str = ''
        if (!id) {
          // 多选
          if (!this.multipleSelection.length) {
            this.$message({
              message: '请选择删除的条目！',
              type: 'warning'
            })
            return false
          }
          // 数据处理
          this.multipleSelection.forEach(item => {
            id_str += item[current.keyName] + ','
          })
        } else {
          id_str = id
        }

        // 提交数据
        this.$confirm('确认删除数据吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.del({ id_str: id_str }, this.apiPath).then(response => {
            // 重新获取列表
            this.getList()
            this.$message({
              message: '删除成功',
              type: 'success'
            })
          })
        })
      },
			// 排序
			updateSort(id = 0, sort = '') {
				this.$api.editSort({
					id: id,
					sort: sort
				}, this.apiPath).then(response => {
					this.$message({
						message: '排序成功',
						type: 'success'
					})
				})
			},
			//启用
			handleShow(id = 0) {
				this.$confirm('确认启用数据吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$api.show({ id_str: id }, this.apiPath).then(response => {
						// 重新获取列表
						this.getList()
						this.$message({
							message: '启用成功',
							type: 'success'
						})
					})
				})
			},
			//禁用
			handleUpshow(id = 0) {
				this.$confirm('确认禁用数据吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					this.$api.upshow({ id_str: id }, this.apiPath).then(response => {
						// 重新获取列表
						this.getList()
						this.$message({
							message: '禁用成功',
							type: 'success'
						})
					})
				})
			}
/*******************************公共方法END******************************/

		}
	}
</script>
